/*
 * @(#)AbstractSyncService.java 2011-11-14
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.sync.service;

import cn.shangjia.common.domain.Domain;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.common.mq.message.DbMessageAction;
import cn.shangjia.common.util.ReflectUtils;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.exception.ShopSyncException;
import cn.shangjia.shop.log.ShopLoggerFactory;

/**
 * syncService基础实现. 并且封装缓存操作.
 * 该类需要注入 两个不同源的DAO
 *
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @version 1.0, 2011-11-14 11:03:08
 * @since 1.0
 */
public abstract  class AbstractSyncService<T extends Domain>  implements SyncService {
	
	protected final Logger logger = ShopLoggerFactory.getLogger(getDomainClass());
	
	private String className;  //
	
	public AbstractSyncService() {
		Class<T> domainClass = getDomainClass();
		className = domainClass.getSimpleName();
	}
	
	@SuppressWarnings({ "unchecked" })
	protected Class<T> getDomainClass() {
		Class<T> domainClass = ReflectUtils.getSuperClassGenricType(this.getClass(), 0);
		return domainClass;
	}
	
	public String getClassName() {
		return this.className;
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.shangjia.shop.sync.service.SyncService#sync(cn.shangjia.common.mq.message.DbMessageAction, java.lang.String)
	 */
	@Override
	public void sync(DbMessageAction action, String id, String userid) throws ShopSyncException {
		
		//验证参数
		if (action == null) 
			throw new ShopSyncException("action is null");
		if (StringUtils.isEmpty(id)) 
			throw new ShopSyncException("id is Empty");
		if (StringUtils.isEmpty(userid))
			throw new ShopSyncException("userid is Empty");
		
		if(action.equals(DbMessageAction.DELETE) || action.equals(DbMessageAction.HARDDELETE)) {
			delete(id, userid);
		}
		if(action.equals(DbMessageAction.INSERT)){
			insert(id, userid);
		}
		if(action.equals(DbMessageAction.UPDATE)){
			update(id, userid);
		}
		
	}
	
	/**
	 *  删除操作
	 * 
	 * @param id
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	protected abstract void delete(String id, String userid);
	
	/**
	 *  插入操作
	 * 
	 * @param id
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	protected abstract void insert(String id, String userid);
	
	/**
	 *  修改操作
	 * 
	 * @param id
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	protected abstract void update(String id, String userid);

}
